package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.BankCheckBillVO;
import com.jishuo.nontax.bfc.service.BankCheckBillService;
import org.dom4j.Element;

import java.math.BigDecimal;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description:
 */
public class BankCheckBillApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if(head.equals("sendBankCheckBill")){ //查询对账流水信息 上传到财政
            String accountcode=body.selectSingleNode("//root/body/accountcode").getText();
            String accountdate=body.selectSingleNode("//root/body/accountdate").getText();
            if(accountcode==null||accountcode.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }
            BankCheckBillService billService = (BankCheckBillService) SpringContextUtil.getBean("bankCheckBillService");
            BaseResult ret = billService.sendBankCheckBill(accountcode, accountdate);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }else if(head.equals("cleanBankCheckBill")){ //清除对账未成功流水信息
            String accountcode=body.selectSingleNode("//root/body/accountcode").getText();
            String paybankdt=body.selectSingleNode("//root/body/paybankdt").getText();
            if(accountcode==null||accountcode.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }

            BankCheckBillService billService = (BankCheckBillService) SpringContextUtil.getBean("bankCheckBillService");
            BaseResult ret = billService.cleanBankCheck(0, accountcode, paybankdt);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }else if(head.equals("setBankCheckBill")){  // 添加修改 记录对账流水信息
            BankCheckBillVO bankCheckBillVO = new BankCheckBillVO();
            bankCheckBillVO.setBankcheckid(body.selectSingleNode("//root/body/bankcheckid").getText());
            bankCheckBillVO.setAccountnum(body.selectSingleNode("//root/body/accountnum").getText());
            bankCheckBillVO.setAccountname(body.selectSingleNode("//root/body/accountname").getText());
            bankCheckBillVO.setAccountdate(body.selectSingleNode("//root/body/accountdate").getText());
            bankCheckBillVO.setAccounttime(body.selectSingleNode("//root/body/accounttime").getText());
            bankCheckBillVO.setSign(Integer.valueOf(body.selectSingleNode("//root/body/sign").getText()));
            bankCheckBillVO.setMoney(new BigDecimal(body.selectSingleNode("//root/body/money").getText()));
            bankCheckBillVO.setSummary(body.selectSingleNode("//root/body/summary").getText());
            bankCheckBillVO.setOpaccountnum(body.selectSingleNode("//root/body/opaccountnum").getText());
            bankCheckBillVO.setOpaccountname(body.selectSingleNode("//root/body/opaccountname").getText());
            bankCheckBillVO.setPurpose(Integer.valueOf(body.selectSingleNode("//root/body/purpose").getText()));
            bankCheckBillVO.setPostscript(body.selectSingleNode("//root/body/postscript").getText());
            if(bankCheckBillVO.getAccountnum()==null||bankCheckBillVO.getAccountnum().equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountnum]");
            }

            BankCheckBillService billService = (BankCheckBillService) SpringContextUtil.getBean("bankCheckBillService");
            BaseResult ret = billService.setBankCheckBill(bankCheckBillVO);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }
        return null;
    }
}
